package com.ayun.member.service.impl;

import com.ayun.util.JwtUtil;
import com.ayun.base.Result;
import com.ayun.member.entity.Staff;
import com.ayun.member.mapper.StaffMapper;
import com.ayun.member.req.StaffREQ;
import com.ayun.member.service.IStaffService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 斌叔
 * @since 2022-05-09
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public Result login(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return Result.error("用户名或密码错误");
        }
        // 1. 通过用户名查询
        Staff staff = getByUsername(username);
        if (staff == null) {
            return Result.error("用户名或密码错误");
        }
        // 2. 验证密码
        if (!new BCryptPasswordEncoder().matches(password, staff.getPassword())) {
            return Result.error("用户名或密码错误");
        }
        // 3. 3. 生成token 响应
        String jwt = jwtUtil.createJWT(staff.getId() + "", staff.getUsername(), true);

        Map<String, String> map = new HashMap<>();
        map.put("token", jwt);
        return Result.ok(map);
    }

    @Override
    public Result search(long page, long size, StaffREQ req) {

        QueryWrapper<Staff> query = new QueryWrapper<>();
        if (req != null) {
            if (StringUtils.isNotBlank(req.getName())) {
                query.like("name", req.getName());
            }
            if (StringUtils.isNotBlank(req.getUsername())) {
                query.like("username", req.getUsername());
            }
        }
        IPage<Staff> data = baseMapper.selectPage(new Page<Staff>(page, size), query);
        return Result.ok(data);
    }

    @Override
    public Result add(Staff staff) {
        if (staff == null || StringUtils.isEmpty(staff.getUsername())) {
            return Result.error("用户名不能为空");
        }
        Staff s = getByUsername(staff.getUsername());
        if (s != null) {
            return Result.error("用户名已存在");
        }

        String password = new BCryptPasswordEncoder().encode(staff.getPassword());
        staff.setPassword(password);
        boolean b = this.save(staff);
        if (b) {
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result getUserInfo(String token) {
        Claims claims = jwtUtil.parseJWT(token);
        if (claims == null || StringUtils.isBlank(claims.getSubject())) {
            return Result.error("token无效");
        }

        //获取用户名
        String username = claims.getSubject();
        //通过用户名查询
        Staff staff = getByUsername(username);
        if (staff == null) {
            return Result.error("用户不存在");
        }

        //2. 将密码设置为null，不响应给前端
        staff.setPassword(null);
        return Result.ok(staff);
    }


    public Staff getByUsername(String username) {
        QueryWrapper<Staff> query = new QueryWrapper<>();
        query.eq("username", username);
        return baseMapper.selectOne(query);
    }

}
